//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//------------------------------------------------------------------------------
//
// This file specifies MBR helper functions. They are publised only to be used
// in EBOOT/SBOOT scenarion to obtain information about downloaded image.
// They also can be used in store module implementation - but in this case
// they are internal store functions.
//
#ifndef __BOOT_STORE_UTILS_H
#define __BOOT_STORE_UTILS_H

#ifdef __cplusplus
extern "C" {
#endif

//------------------------------------------------------------------------------

#ifndef dimof
#define dimof(x)                    (sizeof(x)/sizeof(x[0]))
#endif

//------------------------------------------------------------------------------

BOOL
BootStoreIsThisMBR(
    UINT32 sectorSize,
    UINT8 *pMbrSector
    );

//------------------------------------------------------------------------------

BOOL
BootStoreIsThisLayoutSector(
    UINT32 sectorSize,
    UINT8 *pLayoutSector
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetReservedRegionInfo(
    UINT32 sectorSize,
    UINT8 *pLayoutSector,
    LPCSTR name,
    UINT32 *pStartSector,
    UINT32 *pSectors
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetReservedRegionName(
    UINT32 sectorSize,
    UINT8 *pLayoutSector,
    UINT32 index,
    LPSTR  pNameBuffer,
    UINT32 nameBufferSize
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetFlashRegionInfo(
    UINT32 sectorSize,
    UINT8 *pLayoutSector,
    UINT32 index,
    UINT32 *pSize,
    UINT32 *pCompactSize
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetPartitionType(
    UINT32 sectorSize,
    UINT8 *pMbrSector,
    UINT32 index,
    UINT32 *pType
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetPartitionInfo(
    UINT32 sectorSize,
    UINT8 *pMbrSector,
    UINT32 type,
    UINT32 index,
    UINT32 *pStartSector,
    UINT32 *pSectors
    );

//------------------------------------------------------------------------------

BOOL
BootStoreUpdatePartitionInfo(
    UINT32 sectorSize,
    UINT8 *pMbrSector,
    UINT32 type,
    UINT32 index,
    UINT32 startSector,
    UINT32 sectors
    );

//------------------------------------------------------------------------------

BOOL
BootStoreGetBinFsInfo(
    HANDLE hStore,
    UINT32 *pBase,
    UINT32 *pSize
    );

//------------------------------------------------------------------------------
//
//  Function:  BootStoreBinCompressed
//
//  This function reads and uncompress data to buffer. It returns amount data
//  readed or (-1) on failure. Store should be opened for read and positioned
//  at compressed block start. File (or its part) should be compressed by
//  BinCompress utility.
//
UINT32
BootStoreReadBinCompressed(
    HANDLE hStore,
    VOID *pBuffer,
    UINT32 bufferSize
    );

//------------------------------------------------------------------------------
//
//  Function:  BootStoreReadFromImageLib
//
//  Function reads image from image library. Store should be opened for read
//  and positioned at library begining. Library uses simple format - each image
//  starts with record size (32-bit), followed by position (as RECT) and
//  bin compressed image body. End of library is marked by zero length image.
//  Function returns number of bytes readed or (-1) on failure. Function
//  returns store position to library beginning on exit.
//
UINT32
BootStoreReadFromImageLib(
    HANDLE hStore,
    UINT32 index,
    RECT* pRect,
    VOID* pBuffer,
    UINT32 bufferSize
    );
    
//------------------------------------------------------------------------------

#ifdef __cplusplus
}
#endif

#endif
